<?php
require_once "../config.php";
require_once SESSION_INCLUDE;
require_once DB;
require_once EVENT;

session_start();

//echo stripslashes($_POST['json'])."\n\n";
$json = json_decode(stripslashes($_POST['json']), true);
//var_dump($json);

$db = get_session_db($db_config);

/* Controllo di sessione */
$user = null;
if(!user_is_logged($db))
{	
	header("Location:http://".LOGIN_FILE);
	exit();
}
$user = get_session_user($db);

/* Recupero/Creo l'oggetto Event */
$event_id = null;
if(isset($json['event_id']));
	$event_id = $json['event_id'];
$event = null;
$is_new = false;
if(is_null($event_id))
{
	$event = new Event($db);
	$is_new = true;
	//echo "Nuovo Evento\n<br />";
}	
else
	$event = Event::getFromId($db, $event_id);

/* Modifico i valori */
foreach($json as $key => $value)
{
	if(strcmp($key, "event_hidden") == 0)
	{
		$hidden = 0;
		if(strcmp($value, "on") == 0)
			$hidden = 1;
		$event->set($key, $hidden);
		//echo $key." -> ".$hidden."\n";
	}
	else if(strcmp($key, "event_end") == 0 || strcmp($key, "event_start") == 0)
	{
		$event->set($key, strtotime($value));
		//echo $key." -> ".strtotime($value)."\n";
	}
	else
	{
		if(!strcmp($key, "event_id") == 0)
		{ 
			$event->set($key, $value);
			//echo $key." -> ".$value."\n";
		}
	}
}

$event->set('user_id', $user->getId());

/* Scrivo le modifiche */
if($is_new)
{ 
	$event->store();
	//echo "Stored";
}
else
{ 
	$event->commit();
	//echo "Committed";
}
?>
